<?php

/**
 * Class for render Sign up Box
 * 
 * 
 * @author Nguyen Tuan Quyen <nguyen.tuan.quyen.it@gmail.com>
 * @version 1.0
 * @package common.front_blocks.order_form
 */

class OrderFormBlock extends CWidget
{
    
    //Do not delete these attr block, page and errors
    public $id='order_form';
    public $block=null;     
    public $errors=array();
    public $page=null;
    public $layout_asset='';
        
    
    public function setParams($params){
          return; 
    }
    
    public function run()
    {        
            $this->renderContent();
    }       
 
 
    protected function renderContent()
    {
        
      
	if(isset($this->block) && ($this->block!=null)){	
               $model=new Order;
                // if it is ajax validation request
                if(isset($_POST['ajax']) && $_POST['ajax']==='order-form')
                {
                        echo CActiveForm::validate($model);
                        Yii::app()->end();
                }
                // collect user input data
                if(isset($_POST['Order']))
                {
                        $model->attributes=$_POST['Order'];
                        $model->order_status = ConstantDefine::ORDER_PENDING;
                        // validate user input password
                        if($model->validate()){
                                if($model->save())
                                {
                                	user()->setFlash('success',t('Đặt hàng thành công!'));
                                	$model = new Order;
                                }
                        }
                }
           $this->render(BlockRenderWidget::setRenderOutput($this),array('model'=>$model));
	} else {
	    echo '';
	}
       
    }
    
    public function validate(){	
		return true ;
    }
    
    public function params()
    {
         return array();
    }
    
    public function beforeBlockSave(){
	return true;
    }
    
    public function afterBlockSave(){
	return true;
    }
}

?>